package com.pirestupppai.widgets.dialog;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.adapter.PsListAdapter;
import com.pirestupppai.base.BaseDialog;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.ui.activity.ImageEditActivity;
import com.pirestupppai.ui.activity.SelectPictureActivity;
import com.pirestupppai.utils.DownloadUtil;
import com.pirestupppai.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

import static com.pirestupppai.rxbus.RxBus.RXBUS_DOWNLOAD_RESULT;

/**
 * 帮P对话框
 *
 * @author: xiaoluo
 * @date: 2017-04-25 16:53
 */
public class PsDialog extends BaseDialog implements View.OnClickListener {

    @BindView(R.id.ps_download)
    TextView downloadTv;
    @BindView(R.id.ps_add_inprogress)
    TextView addInprogressTv;
    @BindView(R.id.ps_release)
    TextView releaseTv;
    @BindView(R.id.ps_close_layout)
    LinearLayout closeLayout;

    private FeedItem mFeedItem;
    private List<String> mDownloadList = new ArrayList<>();
    private Subscription mSubscription;
    private ProgressingDialog mDialog;
    private int mCount = 0;

    public PsDialog(Context context, FeedItem feedItem) {
        super(context, R.style.CustomDialogStyle);
        mFeedItem = feedItem;
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.dialog_ps;
    }

    @Override
    protected boolean atBottom() {
        return true;
    }

    @Override
    protected void initViews() {
        if (mFeedItem.getMedias() != null && mFeedItem.getMedias().size() != 0) {
            mDownloadList.add(mFeedItem.getMedias().get(0).getPathname());
        }
        mDialog = new ProgressingDialog(mContext);

        //注册观察者
        mSubscription = RxBus.getInstance()
                .toObservable(RXBUS_DOWNLOAD_RESULT, String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mCount ++;
                        if (mCount == mDownloadList.size()) {
                            if (mDialog != null && mDialog.isShowing()) {
                                mDialog.dismiss();
                            }
                            if (TextUtils.isEmpty(s)) {
                                downloadTv.setClickable(true);
                            } else {
                                Intent intent = new Intent(mContext, ImageEditActivity.class);
                                intent.putExtra(Constant.IntentKey.PS_TYPE, PsListAdapter.PS_TYPE_REPLY_NOW);
                                intent.putExtra(Constant.IntentKey.SELECT_LIST, s);
                                intent.putExtra(Constant.IntentKey.PARENT_ID, mFeedItem.getId());
                                mContext.startActivity(intent);
                                dismiss();
                            }
                        }
                    }
                });
    }

    @Override
    protected void initListeners() {
        downloadTv.setOnClickListener(this);
        releaseTv.setOnClickListener(this);
        addInprogressTv.setOnClickListener(this);
        closeLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ps_download:
                download();
                break;
            case R.id.ps_add_inprogress:
                addInProgress();
                break;
            case R.id.ps_release:
                Intent intent = new Intent(mContext, SelectPictureActivity.class);
                intent.putExtra(Constant.IntentKey.RELEASE_TYPE, Constant.SELECT_TYPE_WORK);
                intent.putExtra(Constant.IntentKey.PS_TYPE, PsListAdapter.PS_TYPE_REPLY_NOW);
                intent.putExtra(Constant.IntentKey.PARENT_ID, mFeedItem.getId());
                mContext.startActivity(intent);
                dismiss();
                break;
            case R.id.ps_close_layout:
                dismiss();
                break;
        }
    }

    /**
     * 添加至进行中
     */
    private void addInProgress() {
        NetworkRequest.getInstance().createMission(mFeedItem.getId(),
                new TupaiSubscriber(mContext) {
                    @Override
                    protected void requestSuccess(Object o) {
                        Utils.showToast("添加到进行中成功");
                    }

                    @Override
                    protected void requestError(String error) {

                    }
                });
        dismiss();
    }

    /**
     * 下载
     */
    private void download() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
        downloadTv.setClickable(false);
        DownloadUtil.downloadImage(mDownloadList);
    }
}
